package nju.mscs.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import nju.mscs.data.object.Database;
import nju.mscs.data.object.Date;
import nju.mscs.data.object.GiftList;
import nju.mscs.data.object.Goods;
import nju.mscs.data.object.Inventory;
import nju.mscs.data.object.PromotionList;
import nju.mscs.data.object.SaleRecord;
import nju.mscs.data.object.User;
import nju.mscs.data.object.UserList;
import nju.mscs.data.object.VipConsumer;
import nju.mscs.data.object.VipList;
import nju.mscs.data.object.VipUpdataList;

public class DataServerManager {
	
	
	public static void main(String args[]){
		Database db = initialize();
		System.out.println(db.searchVipConsumerByTelephone(12423).getName());
	}
	
	//private String fileName;
	//private Database database;
	
	public DataServerManager(String name) throws FileNotFoundException, IOException, ClassNotFoundException{
		//setFileName(name);
		//File file = new File(name);
		//if(file.exists())
			//readData();
	}
	
	public DataServerManager(){
		
	}
		
	/*public void setFileName(String name){
		fileName = name;
	}*/
	
	public Database readData(String name) throws FileNotFoundException, IOException, ClassNotFoundException{
		File file = new File(name);//new File (fileName);
		ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(file));
		return (Database) ois.readObject();
	}

	public void savaDate(String name,Database db) throws FileNotFoundException, IOException{
		File file = new File (name);
		ObjectOutputStream oos =  new ObjectOutputStream(new FileOutputStream(file));
		oos.writeObject(db);
		oos.close();
	}
	
	public static Database initialize(){
		Inventory inventory = new Inventory();
		VipList vipList = new VipList();
		PromotionList promotionList = new PromotionList();
		UserList userList = new UserList();
		VipUpdataList vipUpdataList = new VipUpdataList();
		GiftList giftList = new GiftList();
		SaleRecord saleRecord = new SaleRecord();
		
		Database db = new Database(inventory, vipList, promotionList, userList,vipUpdataList,giftList,saleRecord);
		
		db.addGoods("奥利奥威化", 4, "1", 10);
		Goods good=db.searchGoodsById("1");
		good.setInTime(new Date(2012,10,12));
		good.setOutTime(new Date(2012,12,12));
		db.addUser("王悦", "wangyue", "1", User.SYSTEM_MANAGER,0);
		db.addVipConsumer("王悦", "1", VipConsumer.MALE,12423,new Date());
		db.addGift("雅哈咖啡", "1");
		Date date=new Date(2012,12,12);
		db.addTotalGiftPromotion("1",0, 0, "1",date,date);
		db.addGoodsGiftPromotion("2","1", "1",date,date);
		db.addTotalDiscountPromotion("3", 0, 0, 0.8,date,date);
		db.addGoodsDiscountPromotion("4", "1", 0.7,date,date);
		
		return db;
	}

	/*public Database getDatabase() {
		return database;
	}
	public void setDatabase(Database db){
		this.database = db;
	}*/
}